﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using DevExpress.XtraReports;
using DevExpress.XtraReports.UI;
namespace Exact_VAS
{
    public partial class JournalVoucherForm : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        public JournalVoucherForm(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            this.InitializeData();

        }
        private void InitializeData()
        {
            var queryJournal = from Journal in DS.dagbks
                               
                               select Journal;
            this.dagbkBindingSource.DataSource = queryJournal;


            if (queryJournal.Count() > 0)
            {
                this.lookUpEdit_Journal.EditValue = queryJournal.First().dagbknr;
            }
           
                             
        }

        private void lookUpEdit_Journal_EditValueChanged(object sender, EventArgs e)
        {

            var queryEntry = from Entry in DS.ISHCMC_V_ListEntries
                             where Entry.JournalNumber == Convert.ToString(this.lookUpEdit_Journal.EditValue)
                             select Entry;
            this.iSHCMCVListEntryBindingSource.DataSource = queryEntry;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            List<ISHCMC_JournalVoucher> ListJournalVoucher = new List<ISHCMC_JournalVoucher>();
            if (this.gridView1_EntryNumber.GetSelectedRows().Length > 0)
            {
                foreach (int i in this.gridView1_EntryNumber.GetSelectedRows())
                {
                    ISHCMC_V_ListEntry itemEntry = (ISHCMC_V_ListEntry)this.gridView1_EntryNumber.GetRow(i);
                    var query = from JV in DS.ISHCMC_JournalVouchers
                                where JV.VoucherNo == itemEntry.JournalNumber.Trim() + "." + itemEntry.EntryNumber
                                select JV;
                    foreach (ISHCMC_JournalVoucher itemJV in query)
                    {
                        ListJournalVoucher.Add(itemJV);
                    }
                }

                
               


            }
            if (ListJournalVoucher.Count > 0)
            {
                JournalVoucherRpt rpt = new JournalVoucherRpt();
                rpt.LoadDataSource(ListJournalVoucher);
                ReportPrintTool PrintTool = new ReportPrintTool(rpt);
                PrintTool.ShowPreview();
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            var queryEntry = from Entry in DS.ISHCMC_V_ListEntries
                             where Entry.JournalNumber == Convert.ToString(this.lookUpEdit_Journal.EditValue)
                             select Entry;
            this.iSHCMCVListEntryBindingSource.DataSource = queryEntry;
        }
    }
}